#2019.1.10.
from PIL import Image, ImageDraw, ImageFont, ImageFilter

from langconv import *
# https://github.com/skydark/nstools/tree/master/zhtools


# 转换繁体到简体
def cht_to_chs(line):
    line = Converter('zh-hans').convert(line)
    line.encode('utf-8')
    return line

# 转换简体到繁体
def chs_to_cht(line):
    line = Converter('zh-hant').convert(line)
    line.encode('utf-8')
    return line

#获取文本
scentences = chs_to_cht('学而时习之，不亦说乎？ 有朋自远方来，不亦乐乎？ 人不知，而不愠， 不亦君子乎？').split()#input('请输入文本（空格分割句子）：').split()
scentences_count = len(scentences)

# 创建Font对象:
font = ImageFont.truetype('images/font.ttf', 64)

#打开底版图片
imageFile = 'images/bg.jpg'
im1=Image.open(imageFile)

# 在图片上添加文字 
draw = ImageDraw.Draw(im1)
x_offset, y_offset = 120, 100
i,j = 0, 0
for scentence in scentences:
    j = 0
    for word in scentence:
        x = 780 - x_offset * i
        y = 180 + y_offset * j
        draw.text((x,y),word,(152,4,4),font = font)
        j += 1
    i += 1

'''
for i in range(scentences_count):
    draw.text((780, 180+100*i),scentences[i],(152,4,4),font = font)

draw.text((780, 180),'论',(152,4,4),font=font)
draw.text((780, 280),'语',(152,4,4),font=font)
draw.text((780, 380),'通',(152,4,4),font=font)
draw.text((780, 480),'读',(152,4,4),font=font)
'''

draw = ImageDraw.Draw(im1)

# 在图片上添加另一张图片（分割线、logo）
imageFile = 'images/logo.png'
im2=Image.open(imageFile)
box = (0, 0, 68, 78)
im2_logo = im2.crop(box)

im1.paste(im2_logo,(120,1300,120+68,1300+78))

# 保存
im1.save('target.jpg', 'jpeg')
